package control;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Context;
import org.apache.catalina.core.ApplicationContext;
import org.apache.catalina.core.StandardContext;

import vo.ResultVO;
import vo.bbs.Comment_tVO;



/**
 * Servlet implementation class ControlerServlet
 */
@WebServlet(
		urlPatterns = { "/ControlerServlet" }, 
		initParams = { 
				@WebInitParam(name = "param", value = "/WEB-INF/prop/class.properties")
		})
public class ControlerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	Map<String,Object> classMap = null;
    public ControlerServlet() {
        super();
        // TODO Auto-generated constructor stub
        classMap = new HashMap<String,Object>();
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		
		String paramUrl = config.getInitParameter("param");
		ServletContext  application = config.getServletContext();
		String path = application.getRealPath(paramUrl);
		
		Properties prop = new Properties();
		FileInputStream  fis = null;
		
			try {
				fis = new FileInputStream(path);
				prop.load(fis);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		Iterator<Object> it = prop.keySet().iterator();	
		while(it.hasNext()){
			
			String key = (String)it.next();
			try {
				Object obj = Class.forName(prop.getProperty(key)).newInstance();
				Class.forName("vo.ResultVO").newInstance();
				System.out.println("String key = (String)it.next() : "+key);
				System.out.println("Object obj : "+obj);
				classMap.put(key, obj);
			} catch (Exception ex){
				ex.printStackTrace();
			}
			
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		requestProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		requestProcess(request, response);
	}
	
	protected void requestProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String cmd =request.getParameter("cmd");
		
		if(request.getAttribute("comment_tVO")==null){
			
			request.setAttribute("comment_tVO", new Comment_tVO());
		}
		
		if(cmd == null)
			return;
		System.out.println("cmd : "+cmd);
		System.out.println("(WinNumInter)classMap.get(cmd) : "+(ActionInter)classMap.get(cmd));
		ActionInter mi = (ActionInter)classMap.get(cmd);
		ResultVO vo =mi.excute(request, response);
		
		String path = vo.getUrl();
		if(vo.getCmd().equals("get")){
			response.sendRedirect(path);
		}else if(vo.getCmd().equals("post")){
			RequestDispatcher rd = request.getRequestDispatcher(path);
			rd.forward(request, response);
		}
	}

}
